package com.lzw.breezeNovel.webApp.service.impl.comic;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.comic.Comic;
import com.lzw.breezeNovel.model.entity.pojo.comic.ComicCollection;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.enums.comic.ComicStatus;
import com.lzw.breezeNovel.webApp.mapper.comic.ComicCollectionMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.service.comic.ComicCollectionService;
import com.lzw.breezeNovel.webApp.vo.comic.ComicVo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.comic
 * @date 2025/3/16
 * @description 漫画收藏
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class ComicCollectionServiceimpl implements ComicCollectionService {

    /**
     * 注入mapper
     */
    @Autowired
    private ComicCollectionMapper comicCollectionMapper;

    /**
     * 注入用户信息mapper
     */
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 收藏漫画
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Integer comicId) {
        //获取当前用户信息
        Long id = getCurrentUser();
        //根据用户id和漫画id查询收藏记录
        Long count = comicCollectionMapper.selectCountByUserIdAndComicId(id, comicId);
        if (count > 0) {
            throw new BaseException(ResultCodeEnum.APP_COMIC_COLLECTION_EXIST);
        }
        //添加收藏记录
        ComicCollection comicCollection = new ComicCollection();
        comicCollection.setComicId(comicId);
        comicCollection.setUserId(Math.toIntExact(id));
        comicCollection.setCollectTime(new Date());
        comicCollectionMapper.insert(comicCollection);
    }

    /**
     * 取消收藏
     */
    @Override
    public void cancel(Integer comicId) {
        //获取当前用户信息
        Long id = getCurrentUser();
        //根据用户id和漫画id删除收藏记录
        Long count = comicCollectionMapper.selectCountByUserIdAndComicId(id, comicId);
        if (count == 0) {
            throw new BaseException(ResultCodeEnum. APP_COMIC_CANNER_EXIST);
        }
        comicCollectionMapper.deleteByUserIdAndComicId(id, comicId);
    }

    /**
     * 查询收藏列表
     */
    @Override
    public List<ComicVo> list() {
        //获取当前用户信息
        Long id = getCurrentUser();
        //根据用户id查询收藏记录
        List<Comic> comicList = comicCollectionMapper.selectByUserId(id);
        //拷贝对象
        List<ComicVo> copy = copy(comicList);
        return copy;
    }

    /**
     * 删除收藏
     */
    @Override
    public void delete(Integer comicId) {
        //获取当前用户信息
        Long id = getCurrentUser();
        comicCollectionMapper.deleteByUserId(id,comicId);
    }


    /**
     * 获取当前用户
     */
    @Transactional(readOnly = true)
    private Long getCurrentUser() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断是否存在
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        //获取手机号
        String phone = loginUser.getPhone();
        //根据手机号查询用户信息
        UserInfo userId = userInfoMapper.selectByPhone(phone);
        return userId.getUserId();
    }

    /**
     * 对于漫画对象进行拷贝
     */
    @Nullable
    @Contract(pure = true)
    private List<ComicVo> copy(@NotNull List<Comic> comicList){
        return comicList.stream().map(comic -> {
            //创建ComicVo对象
            ComicVo comicVo = new ComicVo();
            //拷贝属性
            BeanUtils.copyProperties(comic,comicVo);
            //获取漫画的状态
            Integer status = comic.getStatus();
            //根据状态获取描述
            String statusDesc = ComicStatus.getDesc(status);
            //设置状态描述
            comicVo.setStatus(statusDesc);
            //判断是否要收费
            String like = comic.getFree() == 0 ? "免费" : "付费";
            comicVo.setFree(like);
            return comicVo;
        }).collect(Collectors.toList());
    }
}
